package com.wow.wa.bill.serivce;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;

import com.wow.wa.bill.serivce.vo.SystemDTO;

public class CommonComentImpl implements CommonComent {

	private static final String SHAREDPREFERENCES = "SHAREDPREFERENCES";
	private Context ct = null;

	public CommonComentImpl(Context ct) {
		this.ct = ct;
	}

	@Override
	public SystemDTO getSystemDTO() {

		SystemDTO systemDTO = new SystemDTO();

		SharedPreferences sharedPreferences = ct.getSharedPreferences(
				SHAREDPREFERENCES, Context.MODE_WORLD_READABLE);

		systemDTO.setNowMoney(sharedPreferences.getString("nowMoney", "0"));
		systemDTO.setUseMoney(sharedPreferences.getString("useMoney", "0"));

		return new SystemDTO();
	}

	@Override
	public void setSystemDTO(SystemDTO systemDTO) {
		SharedPreferences sharedPreferences = this.ct.getSharedPreferences(
				SHAREDPREFERENCES, Context.MODE_WORLD_WRITEABLE);

		Editor editor = sharedPreferences.edit();
		editor.putString("nowMoney", systemDTO.getNowMoney());
		editor.putString("useMoney", systemDTO.getUseMoney());
		editor.commit();

	}

	@Override
	public void sendMail(String title, String content, String maillAdress,
			Bitmap bitmap) {
		String _PicId = "gamil";
		try {
			FileOutputStream os = this.ct.openFileOutput("Img" + _PicId
					+ ".jpg", Context.MODE_WORLD_READABLE);

			bitmap.compress(CompressFormat.JPEG, 100, os);
			os.close();
			File F = this.ct.getFileStreamPath("Img" + _PicId + ".jpg");
			Uri U = Uri.fromFile(F);
			Intent i = new Intent(Intent.ACTION_SEND);
			i.setType("image/jpg ");
			i.putExtra(Intent.EXTRA_STREAM, U);
			i.putExtra(Intent.EXTRA_SUBJECT, title);
			i.putExtra(Intent.EXTRA_EMAIL, new String[] { maillAdress });
			i.setType("message/rfc822");
			i.putExtra(Intent.EXTRA_TEXT, content);
			this.ct.startActivity(Intent.createChooser(i, "發送訊息至"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void sendMail(String title, String content, String maillAdress) {
		try {

			Intent i = new Intent(Intent.ACTION_SEND);

			i.putExtra(Intent.EXTRA_SUBJECT, title);
			i.putExtra(Intent.EXTRA_EMAIL, new String[] { maillAdress });
			i.setType("text/plain");
			i.putExtra(Intent.EXTRA_TEXT, content);
			this.ct.startActivity(Intent.createChooser(i, "發送訊息至"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void callPhone(String phone) {
		String pphone = "tel:" + phone;
		Intent call = new Intent(Intent.ACTION_CALL, Uri.parse(pphone));
		this.ct.startActivity(call);

	}

	@Override
	public String stringtoMoney(String str) {
		List<String> moneys = new ArrayList<String>();
		String money = str.toString();
		int total = money.length();

		for (int i = money.length(); i > 2; i -= 3) {
			moneys.add(money.substring(i - 3, i));
		}

		if (total % 3 != 0) {
			moneys.add(money.substring(0, total % 3));
		}
		Collections.sort(moneys, Collections.reverseOrder());
		return StringUtils.join(moneys, ",");

	}
}
